<template>
	<view class="index-search">
		<view class="mar">
			<view class="header">
				<u-search @change="search2" @search="search" :showAction='false' placeholder="搜索食谱教材" v-model="ipt1"></u-search>
			</view>
			<view class="search-con" v-if="show">
				<view class="lishi">
					<text style="display: block;color: #999999;">历史搜索</text>
					<view class="jilu-box">
						<text @click="toSearch(item)" v-for="(item,index) in jlList" class="li-item">{{item}}</text>
					</view>
				</view>
				<view class="remen">
					<text style="display: block;color: #999999;">热门食谱</text>

				</view>
			</view>
			<view class="show-shop" v-if="!show">
				<view class="tuijian">
					<view class="tj-con">
						<view class="tj-item" @click="toDetail(item)" v-for="(item,index) in shopList">
							<view class="tj-img">
								<img :src="item.img" alt="">
							</view>
							<view class="tj-title">
								<view class="title-name">
									{{item.shopName}}
								</view>
								<view class="title-user">
									<view class="tj-usermsg">
										<view class="user-img">
											<u-avatar size="20"
												src='http://pic2.sc.chinaz.com/Files/pic/pic9/202002/hpic2119_s.jpg'></u-avatar>
										</view>
										<view class="user-name">
											{{item.user}}
										</view>
									</view>
									<view class="user-kan">
										<u-icon name="eye" color="#bbb" size="20"></u-icon>{{item.kan}}
									</view>
								</view>
							</view>
						</view>
					</view>
				</view>
			</view>
			<u-empty mode="search" icon="http://cdn.uviewui.com/uview/empty/car.png" v-if="show2">
			</u-empty>
		</view>
	</view>
</template>

<script>
	import {
		getceshiDjm
	} from '../../../utils/api.js'
	export default {
		data() {
			return {
				ipt1: '',
				show: true,
				jlList: [],
				shopList: [],
				newList: [],
				show2: false
			}
		},
		methods: {
			toDetail(item){
				uni.navigateTo({
					url:`/pages/tabbar-1-detail/tabbar-1-shopDetail/tabbar-1-shopDetail?id=${item.id}`
				})
			},
			search() {
				uni.setStorageSync('jl', [...this.jlList, this.ipt1])
				let jl = uni.getStorageSync('jl') || []
				this.jlList = jl
				this.shopList = this.newList.filter(item => {
					if (item.shopName.includes(this.ipt1)) {
						this.show = !this.show
						return item
					} else {
						this.show2 = !this.show2
						this.show = !this.show
					}
				})
			},
			search2() {
				if (this.ipt1 == '') {
					this.show2 = false
					this.show = !this.show
				}
			},
			toSearch(items) {
				this.shopList = this.newList.filter(item => {
					if (item.shopName.includes(items)) {
						this.show = !this.show
						this.ipt1 = items
						return item
					} else {
						this.ipt1 = items
						this.show2 = !this.show2
						this.show = !this.show
					}
				})
			},
		},
		onLoad() {
			let jl = uni.getStorageSync('jl') || []
			this.jlList = jl
			getceshiDjm().then(res => {
				this.shopList = res.data
				this.newList = [...this.shopList]
			})
		},
	}
</script>

<style>
	.index-search {
		width: 100%;
	}

	.header {
		width: 100%;
		margin: 0 auto;
		padding: 20rpx 0;
		background-color: #fff;
	}

	.mar {
		width: 95%;
		margin: 0 auto;
	}

	.search-con {
		width: 100%;
		margin-top: 20rpx;
		overflow: hidden;
	}

	.lishi {
		width: 100%;
		padding: 20rpx 0;
		margin-bottom: 20rpx;
	}

	.jilu-box {
		display: flex;
		flex-wrap: wrap;
		width: 100%;
		margin-top: 10rpx;
	}

	.li-item {
		margin: 20rpx;
		padding: 15rpx 20rpx;
		border-radius: 40rpx;
		background-color: gold;
		font-size: 28rpx;
	}

	.remen {
		width: 100%;

	}

	.tuijian {
		width: 100%;
	}

	.tj-con {
		width: 100%;
		column-count: 2;
		column-gap: 0rpx;
	}

	.tj-item {
		display: flex;
		flex-wrap: wrap;
		margin: 10rpx;
		border-radius: 30rpx;
		overflow: hidden;
		counter-increment: item-counter;
		box-shadow: 1rpx 1rpx 15rpx 1rpx rgba(185, 185, 185, 0.5);
	}

	.tj-img {
		width: 100%;
	}

	.tj-img img {
		width: 100%;
	}

	.tj-title {
		width: 90%;
		margin: 0 auto;
	}

	.title-user {
		width: 100%;
		display: flex;
		padding: 20rpx 0;
	}

	.tj-usermsg {
		flex: 1;
		display: flex;
	}

	.title-name {
		font-weight: bold;
		font-size: 36rpx;
	}

	.user-name {
		font-size: 26rpx;
		color: #999999;
		text-indent: 8rpx;
	}

	.user-kan {
		display: flex;
		font-size: 28rpx;
		color: #999999;
	}
</style>